package com.lucenly.card.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lucenly.card.R;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.constants.UrlDefend;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import ezy.ui.layout.LoadingLayout;

public class MsgDetailActivity extends BaseActivity {
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.view)
    LoadingLayout view;
    @BindView(R.id.tv_title1)
    TextView tv_title1;
    @BindView(R.id.tv_content)
    TextView tv_content;
    int id = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_detail);
    }

    @Override
    protected void initView() {
        ll_back.setVisibility(View.VISIBLE);
        tv_title.setText("详情");
        id = getIntent().getIntExtra("id",0);
    }

    @Override
    protected void bindEvent() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        view.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }
    private void getData() {
        ViseHttp.GET(UrlDefend.MSG_DETAIL)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("id",id+"")
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        view.showContent();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            tv_title1.setText(jsonObject.getString("title"));
                            tv_content.setText(jsonObject.getString("content"));
                        } catch (JSONException e) {
                            view.setErrorText("不是Json格式,转换错误");
                            view.showError();
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        ToastUtil.showSingletonShort(errMsg);
                        view.setErrorText(errMsg);
                        view.showError();
                    }
                });
    }
    @Override
    protected void processClick(View view) {

    }
}
